"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 系统内部使用工具
Case Name   : 执行gstrace codepath,指定-o文件已存在
Create At   : 2023/05/16
Owner       : @kyeleze
Description :
    1.启动gstrace
    2.执行gstrace dump
    3.执行gstrace codepath
    4.执行gstrace codepath,指定-o文件已存在;
    5.环境清理
Expect      :
    1.启动trace成功，显示[GAUSS-TRACE] start Success!
    2.成功
    3.成功
    4.成功,原文件被覆盖
    5.清理成功
History     :
"""

import unittest
import os
import time
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Common import Common
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

log = Logger()


class SystemInternalTools(unittest.TestCase):
    def setUp(self):
        log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.PrimaryNode = Node('PrimaryDbUser')
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.common = Common()
        self.dir_path = os.path.join(macro.DB_BACKUP_PATH, "trace0044")
        self.dump_path = os.path.join(macro.DB_BACKUP_PATH, 'trace0044.dump')

    def test_system_internal_tools(self):
        text = '-----step1:启动gstrace;expect:成功-----'
        log.info(text)
        start = f"source {macro.DB_ENV_PATH};" \
                f"gstrace start -p {self.PrimaryNode.db_port}"
        log.info(start)
        start_res = self.common.get_sh_result(self.PrimaryNode, start)
        self.assertIn(self.constant.trace_start_success, start_res,
                      '执行失败：' + text)

        text = '-----step2:gstrace dump;expect:成功-----'
        log.info(text)
        time.sleep(3)
        gs_trace_dump = f"source {macro.DB_ENV_PATH};" \
                        f"gstrace dump -p {self.PrimaryNode.db_port}" \
                        f" -o {self.dump_path}"
        log.info(gs_trace_dump)
        dump_msg = self.common.get_sh_result(self.PrimaryNode, gs_trace_dump)
        self.assertIn("dump Success", dump_msg, '执行失败:' + text)

        text = '-----step3:执行gstrace codepath;expect:成功-----'
        log.info(text)
        time.sleep(3)
        output_file = os.path.join(self.dir_path, "trace0044.txt")
        gstrace_codepath = f"mkdir -p {self.dir_path};" \
                           f"source {macro.DB_ENV_PATH};" \
                           f"gstrace codepath -f " \
                           f"{self.dump_path} -o {output_file}"
        log.info(gstrace_codepath)
        gstrace_codepath = self.common.get_sh_result(self.PrimaryNode,
                                                    gstrace_codepath)
        self.assertIn("codepath Success", gstrace_codepath, '执行失败:' + text)

        text = '-----step4:执行gstrace codepath,指定-o文件已存在;' \
               'expect:成功-----'
        log.info(text)
        time.sleep(1)
        gstrace_codepath = f"source {macro.DB_ENV_PATH};" \
                          f"gstrace codepath -f " \
                          f"{self.dump_path} -o {output_file}"
        log.info(gstrace_codepath)
        gstrace_codepath = self.common.get_sh_result(self.PrimaryNode,
                                                       gstrace_codepath)
        self.assertIn("codepath Success", gstrace_codepath, '执行失败:' + text)

    def tearDown(self):
        text = '-----step5:环境清理;expect:清理成功-----'
        log.info(text)
        time.sleep(1)
        stop = f"source {macro.DB_ENV_PATH};" \
               f"gstrace stop -p {self.PrimaryNode.db_port}"
        log.info(stop)
        stop_res = self.common.get_sh_result(self.PrimaryNode, stop)
        del_file = f"rm -fr {self.dump_path} {self.dir_path}"
        log.info(del_file)
        del_res = self.common.get_sh_result(self.PrimaryNode, del_file)
        self.assertIn('stop Success', stop_res, '执行失败:' + text)
        self.assertEqual('', del_res, '执行失败:' + text)
        log.info(f'-----{os.path.basename(__file__)} end-----')